package cn.lyq.card.ui.address

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.AddressInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.BusTag
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_address_add.*


class AddressAddActivity : BaseActivity() {

    private val mInfo by lazy {
        val temp = intent.getSerializableExtra(KEY_INFO)
        if (temp is AddressInfo) {
            temp
        } else {
            AddressInfo()
        }
    }
    private val mModel by lazy {
        getVM(AddressVM::class.java)
    }

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: AddressInfo) {
            val starter = Intent(context, AddressAddActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_address_add
    }

    override fun initView() {
        super.initView()
        if (mInfo.id == -1) {
            setMyTitle(R.string.str_add_address)
            tvConfirm.setText(R.string.str_confirm_add)
        } else {
            setMyTitle(R.string.str_edit_address)
            tvConfirm.setText(R.string.str_confirm_change)
        }
    }

    override fun initData() {
        super.initData()
        if (mInfo.id != -1) {
            etName.setText(mInfo.trueName ?: "")
            etPhone.setText(mInfo.mobile ?: "")
            etDetail.setText(mInfo.detailsAddress ?: "")
            tvArea.text = mInfo.address ?: ""
        }
    }

    override fun initListener() {
        super.initListener()
        tvArea.setClick {
            AreaSelectFg().show(supportFragmentManager)
        }
        tvConfirm.setClick {
            val userName = etName.text.trim().toString()
            if (userName.isEmpty()) {
                ToastUtils.showShort(etName.hint)
                return@setClick
            }
            val phone = etPhone.text.trim().toString()
            if (phone.isEmpty() || phone.length != 11) {
                ToastUtils.showShort(etPhone.hint)
                return@setClick
            }
            val address = tvArea.text.trim().toString()
            if (address.isEmpty()) {
                ToastUtils.showShort(tvArea.hint)
                return@setClick
            }
            val detail = etDetail.text.trim().toString()
            if (detail.isEmpty()) {
                ToastUtils.showShort(etDetail.hint)
                return@setClick
            }
            mInfo.address = address
            mInfo.detailsAddress = detail
            mInfo.mobile = phone
            mInfo.trueName = userName
            mModel.editAndAdd(mInfo).observe(this, Observer {
                when (it?.status) {
                    Status.LOADING -> showLoading()
                    Status.SUCCESS -> {
                        BusUtils.post(BusTag.ADDRESS_REFRESH)
                        ToastUtils.showShort(if (mInfo.id == -1) "添加成功" else "编辑成功")
                        dismissLoading()
                        finish()
                    }
                    Status.ERROR -> {
                        ToastUtils.showShort(it.message.getMsg())
                        dismissLoading()
                    }
                }
            })
        }
    }

    @BusUtils.Bus(tag = BusTag.AREA_SELECT, threadMode = BusUtils.ThreadMode.MAIN)
    fun areaSelect(area: String) {
        mInfo.address = area
        tvArea.text = area
    }
}